package seller

import (
	"dhfshop/models/seller"
	"time"
	"dhfshop/models/consts"
	"dhfshop/controllers/base"
	"github.com/astaxie/beego/validation"
)

// 合作分销关系管理
type CooperaController struct {
	base.BaseController
}

func (this *CooperaController) List() {
	query,_,fields,order_by,offset,limit,err := this.GetQueryPara()
	if err != nil {
	   this.ServeError(err.Error())
	   return
	}
	l, cnt, err := seller.GetAllCooperations(query,fields,order_by,offset,limit)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	r := struct {
		TotalCount   int64     `json:"total_count"`
		Shop    []*seller.Cooperation `json:"shop"`
	}{TotalCount: cnt, Shop: l}
	this.Data["json"] = r
	this.ServeJSON()
}

func (this *CooperaController) Post() {
	var v seller.Cooperation
	valid := validation.Validation{}
	this.ParseForm(&v)
	valid.Required(v.DistributorId, "distributor_id")
	valid.Required(v.SupplierId, "supplier_id")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return	
	}
	if len(v.StartDateStr) > 3 {
	    v.StartDate, _ = time.Parse(consts.DATETIME_FORMAT,v.StartDateStr)
	}
	if len(v.EndDateStr) > 3 {
	    v.EndDate, _ = time.Parse(consts.DATETIME_FORMAT,v.EndDateStr)
	}
	if len(v.SupplierAgreeTimeStr) > 3 {
	    v.SupplierAgreeTime, _ = time.Parse(consts.DATETIME_FORMAT,v.SupplierAgreeTimeStr)
	}
	if len(v.SellerAgreeTimeStr) > 3 {
	    v.SellerAgreeTime, _ = time.Parse(consts.DATETIME_FORMAT,v.SellerAgreeTimeStr)
	}
	shopId, err := seller.AddCooperation(&v)
	if err != nil {
       this.ServeError(err.Error())
		return
    }
	rsp := struct {
		ShopId    int64        `json:"shop_id"`
	    Created   time.Time    `json:"created"`
	}{ShopId: shopId, Created:time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}

func (this *CooperaController) Put() {
	v := seller.Cooperation{}
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return 
	}
	updateFields, err := this.GetUpdateFields(v, "", "form", []string{""})
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	if len(v.StartDateStr) > 3 {
	    v.StartDate, _ = time.Parse(consts.DATETIME_FORMAT,v.StartDateStr)
	}
	if len(v.EndDateStr) > 3 {
	    v.EndDate, _ = time.Parse(consts.DATETIME_FORMAT,v.EndDateStr)
	}
	if len(v.SupplierAgreeTimeStr) > 3 {
	    v.SupplierAgreeTime, _ = time.Parse(consts.DATETIME_FORMAT,v.SupplierAgreeTimeStr)
	}
	if len(v.SellerAgreeTimeStr) > 3 {
	    v.SellerAgreeTime, _ = time.Parse(consts.DATETIME_FORMAT,v.SellerAgreeTimeStr)
	}
    if err := seller.UpdateCooperationById(&v, updateFields...); err != nil{
		this.ServeError(err.Error())
		return
	}
	rsp := struct {
		    CooperateId int64       `json:"cooperate_id"`
	        Modified   time.Time   `json:"modified"`
	    }{CooperateId:v.CooperateId, Modified:time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}

func (this *CooperaController) Delete() {
	cooperate_id, _ := this.GetInt("cooperate_id")
	if err := seller.DeleteCooperation(int64(cooperate_id)); err == nil {
		rsp := struct {
		    CooperateId   int         `json:"cooperate_id"`
	        Modified      time.Time   `json:"modified"`
	    }{CooperateId:cooperate_id, Modified:time.Now().UTC()}
	    this.Data["json"] = rsp
	} else {
	    this.ServeError(err.Error())
	    return
	}
	this.ServeJSON()
}


